/* TaskList.h
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Header file for the TaskList class, which represents a list of tasks.
*/

#ifndef __MZ_TASK_LIST_H__
#define __MZ_TASK_LIST_H__

#include <list>
#include "Task.h"

#include "Selection.h"

#include <vector>
#include <utility>

using std::list;
using std::vector;
using std::pair;



class TaskList : public list<Task> {
public:
	// Constructors.
	TaskList();
	TaskList(const TaskList &tasks);

	TaskList &operator=(const TaskList &tasks);
	
	// Get or set the name of this list.
	const string &Name() const;
	void SetName(const string &name);

	// Get the selected range.
	const Selection &GetSelection() const { return selection; }
	// Check whether anything is selected.
	bool HasSelection() const { return (selection != -1); }

	// Move the selection.
	void SelectUp();
	void SelectDown();
	void SelectSiblingUp();
	void SelectSiblingDown();
	void SelectParent();
	void CursorUp();
	void CursorDown();

	void SelectRoot();
	void SelectEnd();
	void SelectChange(int delta);
	void SelectRow(int row);
	void SetCursor(int row);
	void SelectAll();

	// Get the number of visible rows.
	int Rows() const;
	// Get the task in the given row.
	const Task &operator[](int row) const;
	// Get the number of children of the given row.
	// The return value is 0 if this row is the end of the tree
	// at its depth.
	int Children(int row) const;
	
	
protected:
	// Select a sibling in the given direction.
	void SelectSibling(void (TaskList::*move)(void));

	// Update the table of visible rows.
	void Update(iterator first, iterator last);
	
	
protected:
	// A row of the display. This keeps track of what rows are visible.
	class Row {
	public:
		Row(iterator it) : it(it), children(1) {}
		
		iterator it;
		int children;
	};
	
	
protected:
	vector<Row> rows;

	// The currently selected range of row indices.
	Selection selection;
};

#endif

/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */

